﻿#include "DeviceRegisterCodec_Register.h"

#include "DataPackage_UdpCodec.h"
#include "RegisterCenter_Device.h"
#include "Device.h"

DeviceRegisterCodec_Register::DeviceRegisterCodec_Register()
{
  setObjectName(getClassNameFirstLow<DeviceRegisterCodec_Register>());
}

DeviceRegisterCodec_Register::~DeviceRegisterCodec_Register()
{

}

DataPackage_UdpCodec* DeviceRegisterCodec_Register::decode(DataPackage_UdpCodec* dataPackageInfo_Device)
{
  int deviceId=dataPackageInfo_Device->getDeviceId();
  registerCenter_Device->deviceLogout(deviceId);//若该设备ID已登录，则先将其注销
  registerCenter_Device->deviceLogin((new Device())//登录新设备
                                     ->setHostAddress(dataPackageInfo_Device->getSenderAddress())
                                     ->setPort(dataPackageInfo_Device->getSenderPort())
                                     ->setDeviceId(deviceId));
  logSaver->saveLog("DeviceRegisterCodec,deviceId "+QString::number(deviceId)+" verify successful");
  return dataPackageInfo_Device->toAck(QByteArray(Register&0xff,1));//返回注册成功
}

RegisterCenter_Device* DeviceRegisterCodec_Register::getRegisterCenter_Device() const
{
  return registerCenter_Device;
}

DeviceRegisterCodec_Register* DeviceRegisterCodec_Register::setRegisterCenter_Device(RegisterCenter_Device* const registerCenter_Device)
{
  this->registerCenter_Device=registerCenter_Device;
  printInjection(this,registerCenter_Device);
  return this;
}

void DeviceRegisterCodec_Register::objectAutoInjection(QHash<QString,QObject*>* objMap)
{
  setRegisterCenter_Device(getObjFromMap<RegisterCenter_Device>(objMap));
}
